package com.xdf.zipperdye.controller;

import com.xdf.zipperdye.model.Plan;
import com.xdf.zipperdye.service.PlanService;
import com.xdf.zipperdye.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(description = "计划单")
@CrossOrigin
@RestController
@RequestMapping("/plan")
public class PlanController {
    @Autowired
    private PlanService service;

    @ApiOperation(value="根据主键删除", notes="根据主键删除。返回 Integer", produces="application/json")
    @GetMapping("/deleteByPrimaryKey/{iid}")
    public JsonResult deleteByPrimaryKey(@PathVariable Integer iid){
        return JsonResult.ok(service.deleteByPrimaryKey(iid));
    }

    @ApiOperation(value="完全新增", notes="完全新增。返回 Integer", produces="application/json")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody Plan record){
        return JsonResult.ok(service.insert(record));
    }

    @ApiOperation(value="选择性新增", notes="选择性新增。返回 Integer", produces="application/json")
    @PostMapping("/insertSelective")
    public JsonResult insertSelective(@RequestBody Plan record){
        return JsonResult.ok(service.insertSelective(record));
    }

    @ApiOperation(value="根据主键查询", notes="根据主键查询", produces="application/json")
    @GetMapping("/selectByPrimaryKey/{iid}")
    public JsonResult selectByPrimaryKey(@PathVariable Integer iid){
        return JsonResult.ok(service.selectByPrimaryKey(iid));
    }

    @ApiOperation(value="选择性修改", notes="选择性修改", produces="application/json")
    @PostMapping("/updateByPrimaryKeySelective")
    public JsonResult updateByPrimaryKeySelective(@RequestBody Plan record){
        return JsonResult.ok(service.updateByPrimaryKeySelective(record));
    }

    @ApiOperation(value="完全修改", notes="完全修改", produces="application/json")
    @PostMapping("/updateByPrimaryKey")
    public JsonResult updateByPrimaryKey(@RequestBody Plan record){
        return JsonResult.ok(service.updateByPrimaryKey(record));
    }

    @ApiOperation(value="根据起始、终止日期查询", notes="请求示例：http://localhost:7075/zipperdye/plan/getBetweenDate/2022-01-01/2022-12-01", produces="application/json")
    @GetMapping("/getBetweenDate/{sdate}/{edate}")
    public JsonResult getBetweenDate(@DateTimeFormat(pattern = "yyyy-MM-dd") @PathVariable Date sdate,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd") @PathVariable Date edate){
        return JsonResult.ok(service.getBetweenDate(sdate, edate));
    }

    @ApiOperation(value="返回最后一单", notes="返回最后一单", produces="application/json")
    @GetMapping("/getLast")
    public JsonResult getLast(){
        return JsonResult.ok(service.getLast());
    }
}
